import sys
import pygame
from settings import Settings
from rain_drop import Rain_Drop


class Rain_Image:
    """管理游戏资源和行为的类"""
    def __init__(self):
        pygame.init()
        self.clock = pygame.time.Clock()
        self.settings = Settings()
        self.screen = pygame.display.set_mode \
            ((self.settings.screen_width, self.settings.screen_height))

        pygame.display.set_caption("Drop Image")
        self.drop = Rain_Drop(self)
        self.drops = pygame.sprite.Group()

        self._create_drops()

    def _create_new_row(self):
        """在最后一行消失以后，创建新的一行雨滴"""
        drop = Rain_Drop(self)
        drop_width, drop_height = drop.rect.size

        current_x = drop_width
        # 新的一行所处的位置应该是紧跟上一个雨滴阵列的
        current_y = 2 * drop_height
        while current_x < (self.settings.screen_width - 2 * drop_width):
            self._create_drop(current_x, current_y)
            current_x += 2 * drop_width

    def _create_drops(self):
        """创建一个排列整齐的雨滴"""
        drop = Rain_Drop(self)
        drop_width, drop_height = drop.rect.size

        current_x, current_y = drop_width, drop_height
        while current_y < (self.settings.screen_height - 2 * drop_height):
            while current_x < (self.settings.screen_width - 2 * drop_width):
                self._create_drop(current_x, current_y)
                current_x += 2 * drop_width

            current_x = drop_width
            current_y += 2 * drop_height

    def _create_drop(self, x_position, y_position):
        """创建一个星星并将其放在当前行中"""
        # current_x表示我们要在屏幕上打印的下一个雨滴的水平位置
        new_drop = Rain_Drop(self)

        # 这句话给下一个新的雨滴创建的位置是位于当前这一行对应雨滴的下面
        # 这决定了雨滴是左右移动的还是上下移动的
        new_drop.y = y_position
        new_drop.rect.x = x_position
        new_drop.rect.y = y_position
        self.drops.add(new_drop)

    def _check_events(self):
        """响应按键和鼠标事件"""
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    sys.exit()

    def _update_screen(self):
        """更新屏幕上的图像，并切换到新屏幕"""
        # 每次循环时都会重新绘制屏幕
        self.screen.fill(self.settings.bg_color)
        self.drops.draw(self.screen)
        pygame.display.flip()

    def run_game(self):
        """开始游戏的主循环"""
        # 设置循环的目的是不断的更新并打印运行窗口
        while True:
            self._check_events()
            self._update_drops()
            self._update_screen()
            self.clock.tick(60)

    def _update_drops(self):
        """检查是否有外星人位于屏幕边缘，并更新雨滴的位置"""
        self.drops.update()
        flag = False
        for drop in self.drops.copy():
            if drop.check_edges():
                self.drops.remove(drop)
                flag = True
        if flag:
            self._create_new_row()

    def drops_move(self):
        """将所有雨滴向下移动"""
        for drop in self.drops.sprites():
            drop.rect.y += self.settings.rain_drop_speed


if __name__ == '__main__':
    rd = Rain_Image()
    rd.run_game()
